version 13

set more off
capture log close
capture clear


*log using National_bank_analysis1.txt, text replace
/***!***!***!***!***!*** [National_bank_analysis1_plots1.do ] ***!***!***!***!***!
*
* Project: National Banks 		
* Programmer:  Scott Fulford
*
* Date:    	3/12/14
*
* Auditor:      
* Audit Date:   
*
* Purpose:      
* 1) Make additional plots and figures of National Banks
* 2) 
* 3)
* Inputs:
	National_Banks_counties1890_addvar
*
* Ouputs: 
*		
*
*
***!***!***!***!***!***!***!***!***!***!***!***!***!***!***!***!***/


/***Define Global Directory ****/
	local INDIR "$ROOTDIR\Research\National_Banks\Intermediate"
	local PROGDIR  "$ROOTDIR\Research\National_Banks\Programs"
	local OUTDIR  "$ROOTDIR\Research\National_Banks\Intermediate"
	local GRAPHDIR "$ROOTDIR\Research\National_Banks\Intermediate"
	local TEXDIR "$ROOTDIR\Research\National_Banks\Intermediate"
	
		
/*******************************/

local outputfile reduced_form_regs_analysis4
/**** Min population
Exclude county/years that have population less than
****/
local minpop = 1000
local thissample allrural
local marginal_tpop = 14755 /**** Hardcode it in so it is not changing**/


/****Neighbor analysis ***/
/*Note: neighbors likely understate effect if there are spillovers (which there are)
since effect of bank does not stop a boundaries
That means comparison group is also benefitting from treatment
*/

cd "`INDIR'"
clear
import delimited county_1890_neighbors.txt
/*The neighbor analysis in ArcMap seems not to have gotten confused by multiple
polygons for some counties*/
duplicates list src_gisjoin2 nbr_gisjoin2 
rename src_gisjoin2 gisjoin1890
tempfile neighbors
save `neighbors', replace

use National_Banks_counties1890_addvar, clear
capture drop __000001
keep if year >=1870 & year <=1900


gen Ibanks50c = (banks50c>0 & banks50c<.) if banks50c <.
gen DIbanks50c = D.Ibanks50c


keep if DIbanks50c == 1 | DIbanks50c == -1
tempfile getbanks
save `getbanks', replace
/*These are the counties that gained or lost a bank
Find the neighboring counties for each year
*/

merge m:m gisjoin1890 using `neighbors'
sort year nbr_gisjoin2
keep if _merge ==3
duplicates drop year nbr_gisjoin2, force
keep year nbr_gisjoin2
//drop gisjoin1890 _merge
rename nbr_gisjoin2 gisjoin1890
//gen  gisjoin1890 =nbr_gisjoin2
append using `getbanks'
keep year gisjoin1890
duplicates drop year gisjoin1890, force
merge 1:1 year gisjoin1890 using National_Banks_counties1890_addvar
gen neighbors = 1 if _merge ==3

replace ln_tval_pc = ln_tval_pc*100
capture drop __000001
keep if year >=1860 & year <=1910


rename insample_`thissample' insample
replace insample = insample & (tpop > `minpop' & tpop<.)

gen Ibanks50c = (banks50c>0 & banks50c<.) if banks50c <.

gen capitalstock50pc = capitalstock_pc*`marginal_tpop' / 50


cd "`OUTDIR'"
capture erase `outputfile'.xml
capture erase `outputfile'.txt

sort gisjoin1890 year



/*All neighbor counties that gain or lose a bank 50 bank*/
reg D.ln_tval_pc D.Ibanks50c i.year if neighbors==1 & insample , vce(cluster statenum)
outreg2 using `outputfile', keep(D.Ibanks50c  ) ctitle("neighbors", "all 50")  /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons noni replace

/*Only counties that have one or less 50 bank*/
reg D.ln_tval_pc D.Ibanks50c i.year if neighbors==1 & insample & banks50c <=1 & L.banks50c<=1 & banks<=1 & L.banks<=1 , vce(cluster statenum)
outreg2 using `outputfile', keep(D.Ibanks50c  ) ctitle("neighbors", "only one 50") /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons noni

/*Only counties that have 200 capital or less*/
reg D.ln_tval_pc D.Ibanks50c i.year if neighbors==1 &  insample &  capitalstock <=200 & L.capitalstock <=200  , vce(cluster statenum)
outreg2 using `outputfile', keep(D.Ibanks50c  ) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons noni

/*All neighbor counties that gain or lose a bank 50 bank*/
reg D.ln_tval_pc D.capitalstock50pc i.year if neighbors==1 & insample & capitalstock50pc <=5 & L.capitalstock50pc <=5 , vce(cluster statenum)
outreg2 using `outputfile', keep(D.capitalstock50pc  ) ctitle("neighbors", "all 50")  /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons noni

/*Only counties that have one or less 50 bank*/
reg D.ln_tval_pc D.capitalstock50pc i.year if neighbors==1 & insample & banks50c <=1 & L.banks50c<=1 & banks<=1 & L.banks<=1 & capitalstock50pc <=5 & L.capitalstock50pc <=5 , vce(cluster statenum)
outreg2 using `outputfile', keep(D.capitalstock50pc  ) ctitle("neighbors", "only one 50") /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons noni

/*Only counties that have 200 capital or less*/
reg D.ln_tval_pc D.capitalstock50pc i.year if neighbors==1 &  insample &  capitalstock <=200 & L.capitalstock <=200 & capitalstock50pc <=5 & L.capitalstock50pc <=5 , vce(cluster statenum)
outreg2 using `outputfile', keep(D.capitalstock50pc  ) ctitle("neighbors", "200 or less") /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons noni	
	
	
/*Positive different than negative */
gen DIbanks50c_neg = D.Ibanks50c
replace DIbanks50c_neg = 0 if D.Ibanks50c >=0 & D.Ibanks50c <.
reg D.ln_tval_pc D.Ibanks50c DIbanks50c_neg i.year if neighbors==1 & insample , vce(cluster statenum)
/*
outreg2 using `outputfile', keep(D.Ibanks50c DIbanks50c_neg ) ctitle("neighbors", "pos/neg") /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons noni
	*/
/*Restrict comparison to only counties that gained/lost a bank or did not have one*/
reg D.ln_tval_pc D.Ibanks50c i.year if neighbors==1 & insample & (D.Ibanks50c !=0 | banks==0 ) , vce(cluster statenum)


use National_Banks_counties1890_addvar, clear 


cd "`INDIR'"
use National_Banks_counties1890_addvar, clear 
replace ln_tval_pc = ln_tval_pc*100
capture drop __000001
keep if year >=1860 & year <=1910
/*** Establish comparison population for calculations *****/
by gisjoin1890: egen maxbanks50c = max(banks50c)

/****Establish the comparison marginal population here ****/
sum tpop if insample_allrural & tpop >1000 & year >=1870 & year <=1900 & capitalstock <=50 & maxbanks50c>0


rename insample_`thissample' insample
replace insample = insample & (tpop > 1000 & tpop<.)

gen capitalstock50 = capitalstock/50	
sum tpop if insample
gen capitalstock50pc = capitalstock_pc*`marginal_tpop' / 50
gen capitalstock50pc2 = capitalstock50pc^2

/*What is the difference over all counties*/
/*Reduced form regressions*/
gen less50 = (capitalstock<=50)
by gisjoin1890: egen everless50 = max(less50)

/*Pure reduce form regressions*/
reg ln_tval_pc capitalstock50pc i.year if capitalstock<=50 &  capitalstock50pc<=5 & insample 
outreg2 using `outputfile', keep(capitalstock50pc ) ctitle(50 all) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons
xtreg ln_tval_pc capitalstock50pc i.year if capitalstock<=50 &  capitalstock50pc<=5 & insample 
outreg2 using `outputfile', keep(capitalstock50pc ) ctitle(50 fe) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons
reg D.ln_tval_pc D.capitalstock50pc i. year if capitalstock<=50 & L.capitalstock<=50 &  capitalstock50pc<=5 & L.capitalstock50pc<=5 & insample
outreg2 using `outputfile', keep(D.capitalstock50pc ) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons 
	
reg ln_tval_pc banks50c i. year if capitalstock<=50  & insample 
outreg2 using `outputfile', keep(banks50c ) ctitle(50 all) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons
xtreg ln_tval_pc banks50c i. year if capitalstock<=50 & L.capitalstock<=50 & insample, fe
outreg2 using `outputfile', keep(banks50c ) ctitle(50 fe) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons
reg D.ln_tval_pc D.banks50c i. year if capitalstock<=50 & L.capitalstock<=50 & insample
outreg2 using `outputfile', keep(D.banks50c ) ctitle(50 fe) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons

	
xtreg ln_tval_pc capitalstock50pc i. year if capitalstock<=50 & L.capitalstock<=50 &  capitalstock50pc<=5 & L.capitalstock50pc<=5 & insample, fe
reg D.ln_tval_pc D.capitalstock50pc i. year if capitalstock<=50 & L.capitalstock<=50 &  capitalstock50pc<=5 & L.capitalstock50pc<=5 & insample

reg D.ln_tval_pc D.capitalstock50 i.year if capitalstock<=50 & L.capitalstock<=50 & insample, vce(cluster statenum)
xtreg ln_tval_pc capitalstock50pc i.year if capitalstock<=50 & L.capitalstock<=50 & insample, fe vce(cluster statenum)

	
reg D.ln_tval_pc D.capitalstock50 i.year if capitalstock<=100 & L.capitalstock<=100 & insample, vce(cluster statenum)
xtreg ln_tval_pc capitalstock50 i.year if capitalstock<=100 & L.capitalstock<=100 & insample, fe vce(cluster statenum)
reg D.ln_tval_pc D.capitalstock50 i.year if capitalstock<=400  & L.capitalstock<=400 & insample, vce(cluster statenum)


xtreg ln_tval_pc capitalstock50pc i.year if capitalstock<=400 & L.capitalstock<=400 & insample, fe vce(cluster statenum)
outreg2 using `outputfile', keep(capitalstock50pc ) ctitle(400 all) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons

/***Examine endogenous effect when not constrained***/
reg ln_tval_pc capitalstock50pc  i.year ///
 if capitalstock<=500  & L.capitalstock<=500 & capitalstock50pc <=5 & capitalstock50pc >0 & insample & tpop>1000,   vce(cluster statenum)	
outreg2 using `outputfile', keep(capitalstock50pc ) ctitle(>=50) /// 
	addtext(Year FE, YES) excel stats(coef se ci)  paren(se) auto(2)  nocons
xtreg ln_tval_pc capitalstock50pc  i.year ///
	if capitalstock<=500  & L.capitalstock<=500 & capitalstock50pc <=5 & capitalstock50pc >0 & insample & tpop>1000, fe  vce(cluster statenum)
outreg2 using `outputfile', keep(capitalstock50pc ) ctitle(>=50 fe) /// 
	addtext(Year FE, YES) excel stats(coef se ci)  paren(se) auto(2)  nocons
reg D.ln_tval_pc D.capitalstock50pc  i.year ///
 if capitalstock<=500  & L.capitalstock<=500 & capitalstock50pc <=5 & capitalstock50pc >0 & insample & tpop>1000,   vce(cluster statenum)
outreg2 using `outputfile', keep(D.capitalstock50pc ) ctitle(>=50 D) /// 
	addtext(Year FE, YES) excel stats(coef se ci)  paren(se) auto(2)  nocons

 
/*Allow for non-linear effect*/
/*Calculate value at margin of 24.359 optimal capital */
local marginal_capital = 22.772
local marginal_50 = 22.772/50
local marginal_200 = 200/50

local thiscmd reg ln_tval_pc capitalstock50pc c.capitalstock50pc#c.capitalstock50pc i.year ///
 if capitalstock<=500  & L.capitalstock<=500 & capitalstock50pc <=5 & insample & tpop>1000,  vce(cluster statenum)
`thiscmd'
outreg2 using `outputfile',  ctitle(non-linear) /// 
	addtext(Year FE, YES) excel stats(coef se)  paren(se) auto(2)  nocons	
margins, at(capitalstock50pc =`marginal_50') dydx(capitalstock50pc) post
outreg2 using `outputfile', ctitle(non-linear margins) /// 
	addtext(Year FE, YES) excel stats(coef se ci) paren(se) auto(2)  nocons
`thiscmd'
margins, at(capitalstock50pc =`marginal_200') dydx(capitalstock50pc) post
outreg2 using `outputfile', ctitle(non-linear margins) /// 
	addtext(Year FE, YES) excel stats(coef se ci) paren(se) auto(2)  nocons

local thiscmd xtreg ln_tval_pc capitalstock50pc c.capitalstock50pc#c.capitalstock50pc i.year ///
 if capitalstock<=500  & L.capitalstock<=500 & capitalstock50pc <=5 & insample & tpop>1000, fe  vce(cluster statenum)
`thiscmd'
outreg2 using `outputfile',  ctitle(non-linear) /// 
	addtext(Year FE, YES) excel stats(coef se)  paren(se) auto(2)  nocons	
margins, at(capitalstock50pc =`marginal_50') dydx(capitalstock50pc) post
outreg2 using `outputfile', ctitle(non-linear margins) /// 
	addtext(Year FE, YES) excel stats(coef se ci) paren(se) auto(2)  nocons
`thiscmd'
margins, at(capitalstock50pc =`marginal_200') dydx(capitalstock50pc) post
outreg2 using `outputfile', ctitle(non-linear margins) /// 
	addtext(Year FE, YES) excel stats(coef se ci) paren(se) auto(2)  nocons
/*
local thiscmd reg D.ln_tval_pc D.capitalstock50pc D.capitalstock50pc2 i.year if capitalstock<=400 & L.capitalstock<=400 & insample & tpop>1000,  vce(cluster statenum)
`thiscmd'
outreg2 using `outputfile',  ctitle(non-linear) /// 
	addtext(Year FE, YES) excel stats(coef se)  paren(se) auto(2)  nocons
margins, at(D.capitalstock50pc =`marginal_50') dydx(D.capitalstock50pc) post
outreg2 using `outputfile', ctitle(non-linear margins) /// 
	addtext(Year FE, YES) excel stats(coef se ci) paren(se) auto(2)  nocons
quietly `thiscmd'
margins, at(D.capitalstock50pc =`marginal_200') dydx(D.capitalstock50pc) post
outreg2 using `outputfile', ctitle(non-linear margins) /// 
	addtext(Year FE, YES) excel stats(coef se ci) paren(se) auto(2)  nocons
*/

/**
Note: the calculation is for marginal is that the variable is:
(capital/pop)*(marginal pop / 50) so for a county with the marginal pop
that reduces to capital/50. Then for b x + b2 x^2 the marginal effect is
b+2 *b2 x and so x is capital/50 
*/
/*
matrix b = e(b)
disp "Marginal effect for marginal county: " b[1,1] + 2* b[1,2]*24.359/50
disp "Marginal effect for 200 county: " b[1,1] + 2* b[1,2]*200/50
reg D.ln_tval_pc D.capitalstock50pc D.capitalstock50pc2 i.year if capitalstock<=400 & L.capitalstock<=400 & insample & tpop>1000,  vce(cluster statenum)
matrix b = e(b)
disp "Marginal effect for marginal county: " b[1,1] + 2* b[1,2]*24.359/50
disp "Marginal effect for 200 county: " b[1,1] + 2* b[1,2]*200/50
*/




/*Create variables that will select sample of counties that get a bank this period
or will get one next period*/
gen Ibanks50c = (banks50c>0 & banks50c<.) if banks50c <.
gen FIbanks50c = F.Ibanks50c
gen LIbanks50c = L.Ibanks50c

gen comp1 = 0 if year >=1870 & year <=1900
replace comp1 = 1 if (Ibanks50c ==1 | FIbanks50c == 1 | LIbanks50c ==1) & year >=1870 & year <=1900 & capitalstock<=200 & L.capitalstock<=200
sort year gisjoin1890
by year: sum tpop if comp1==1 & insample
sort gisjoin1890 year
reg D.ln_tval_pc D.Ibanks50c i.year if comp1==1 & insample, vce(cluster statenum)
outreg2 using `outputfile', keep(D.Ibanks50c ) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons 

xtreg ln_tval_pc Ibanks50c i.year if comp1==1 & insample, fe vce(cluster statenum)


/* Whether getting a 50 bank different from losing one*/
gen DIbanks50c_neg = D.Ibanks50c
replace DIbanks50c_neg = 0 if D.Ibanks50c >=0 & D.Ibanks50c <.
reg D.ln_tval_pc D.Ibanks50c DIbanks50c_neg i.year if comp1 ==1 & insample, vce(cluster statenum)
/*
outreg2 using `outputfile', keep(D.Ibanks50c DIbanks50c_neg ) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons
*/

/*Now examine only if have exactly one bank 50c*/
gen Ibank50c = (banks50c==1 & banks50c<.) if banks50c <.
gen FIbank50c = F.Ibank50c
gen LIbank50c = L.Ibank50c
gen comp2 = 0 if year >=1870 & year <=1900
replace comp2 = 1 if (Ibank50c ==1 | FIbank50c == 1 | LIbank50c ==1 ) & year >=1870 & year <=1900
sort year gisjoin1890
by year: sum tpop if comp2==1 & insample
sort gisjoin1890 year
reg D.ln_tval_pc D.Ibank50c i.year if comp2==1 & insample, vce(cluster statenum)
outreg2 using `outputfile', keep(D.Ibank50c ) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons
xtreg ln_tval_pc Ibank50c i.year if comp2==1 & insample, fe vce(cluster statenum)

reg D.ln_tval_pc D.capitalstock50pc i.year if comp1==1 & insample & capitalstock50pc<=5 & L.capitalstock50pc<=5, vce(cluster statenum)
outreg2 using `outputfile', keep(D.capitalstock50pc ) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons	ctitle("panel", "have 50 bank") 
	

/* Whether getting a 50 bank different from losing one*/
gen DIbank50c_neg = D.Ibank50c
replace DIbank50c_neg = 0 if D.Ibank50c >=0 & D.Ibank50c <.
reg D.ln_tval_pc D.Ibank50c DIbank50c_neg i.year if comp2 ==1 & insample, vce(cluster statenum)
/*
outreg2 using `outputfile', keep(D.Ibank50c DIbank50c_neg ) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons
*/
/*Now examine only if have exactly one bank 50c*/

reg D.ln_tval_pc D.Ibank50c i.year if comp2==1 & insample & banks<=1 & L.banks<=1 & capitalstock<=50 & L.capitalstock<=50, vce(cluster statenum)
outreg2 using `outputfile', keep(D.Ibank50c ) ctitle(lag/lead, one 50 bank) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons

reg D.ln_tval_pc D.capitalstock50pc i.year if comp1==1 & insample & capitalstock<=100 & L.capitalstock<=100 & capitalstock50pc<=5 & L.capitalstock50pc<=5, vce(cluster statenum)
outreg2 using `outputfile', keep(D.capitalstock50pc ) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons	ctitle("panel", "have 50 bank") 
	
reg D.ln_tval_pc D.capitalstock50pc i.year if comp2==1  & insample & capitalstock<=50 & L.capitalstock<=50 & capitalstock50pc<=5 & L.capitalstock50pc<=5, vce(cluster statenum)
outreg2 using `outputfile', keep(D.capitalstock50pc ) /// 
	addtext(Year FE, YES) excel stats(coef se ci) bracket(ci) paren(se) auto(2)  nocons	ctitle("panel", "only one bank") 	



/*Make histogram plots*/



/*Some of the banks had not fully capitalized by 1902, replace with 25 if less than 25 but positive*/
replace capitalstock1902 =25 if capitalstock1902 <25 & capitalstock1902 >0

twoway (histogram capitalstock if capitalstock <= 200 & insample & year == 1870, width(10) frequency), ///
	name(hist1870, replace) title("1870",  box bexpand fcolor(none) ) ///
	legend(off) xtitle("") ytitle("") xlabel(none) xmlabel(none) fysize(90)

	
twoway (histogram capitalstock if capitalstock <= 200 & insample & year == 1880, width(10) frequency), ///
	name(hist1880, replace) title("1880",  box bexpand fcolor(none) ) ///
	legend(off) xtitle("") ytitle("") xlabel(none) xmlabel(none) ylabel(none) ymlabel(none) fysize(90)
	
twoway (histogram capitalstock if capitalstock <= 200 & insample & year == 1890, width(10) frequency), ///
	name(hist1890, replace) title("1890",  box bexpand fcolor(none) ) ///
	legend(off) xtitle("") ytitle("") fysize(100)
	
twoway (histogram capitalstock if capitalstock <= 200 & insample & year == 1900, width(10) frequency) ///
	(histogram capitalstock1902 if capitalstock1902 <= 200 & insample & year ==1900, width(10) fcolor(none) lcolor(green) frequency), ///
	name(hist1900, replace) title("1900 and 1902 (outline)",  box bexpand fcolor(none) ) ///
	legend(off) ytitle("") ylabel(none) ymlabel(none) fysize(100)

graph combine hist1870 hist1880 ///
		hist1890 hist1900,  iscale(*1.3)   ///
  cols(2)  imargin(0 0 0 0) ycommon xcommon ///
  b1title("County national bank capital stock", size(*1)) l1title("Number of counties", size(*1)) ///
  name(dist_banks, replace)	ysize(3.5)

cd "`GRAPHDIR'"
graphexportpdf Hist_bank_capitalstock, name(dist_banks)

/***Create scatter plots ****/

/*Per capita capitalstock*/
cd "`INDIR'"
use National_Banks_counties1890_addvar, clear
capture drop __000001
keep if year >=1870 & year <=1900
gen capitalstock50pc = capitalstock_pc*`marginal_tpop' / 50

twoway__histogram_gen capitalstock50pc ///
 if capitalstock50pc>=0 & capitalstock50pc<=6 & capitalstock>=0 & capitalstock<=300 & tpop>1000, ///
 start(-.25) width(.5) frequency  gen(bank_freq capitalstockbin )

reg ln_tval_pc i.year if capitalstock50pc>=0 & capitalstock50pc<=6 & capitalstock>=0 & capitalstock<=300 & tpop>1000
predict demean_ln_tval_pc, resid
replace demean_ln_tval_pc = demean_ln_tval_pc + _b[_cons]
lpoly demean_ln_tval_pc capitalstock50pc ///
	if capitalstock50pc>=0 & capitalstock50pc<=6  & capitalstock>=0 & capitalstock<=300, ///
		at(capitalstockbin) kernel(rectangle) nograph gen( mean_ln_tval_pc)

/*Do scatter plots against capital stock*/
twoway (scatter  mean_ln_tval_pc capitalstockbin [fw= bank_freq], ///
	mcolor(forest_green*.8) ///
	title("Log total production per capita",  box bexpand fcolor(none) size(*.9)) ///
	xtitle("Capital stock per person" "(in multiples of $50,000 in marginal county pop.)", size(*1.2)) ytitle("") ylabel(none) ymlabel(none)), ///
	name(scatterprod_capitalpc, replace)

cd "`GRAPHDIR'"
graphexportpdf Scatter_prod_capital, name(scatterprod_capitalpc)


cd "`INDIR'"
use National_Banks_counties1890_addvar, clear
capture drop __000001
keep if year >=1870 & year <=1900
twoway__histogram_gen capitalstock if capitalstock>=0 & capitalstock<=300, frequency start(-2.5) width(5) gen(bank_freq capitalstockbin )
keep capitalstockbin bank_freq
keep if capitalstockbin <.
tempfile hist1
save `hist1', replace


use National_Banks_counties1890_addvar, clear
capture drop __000001
keep if year >=1870 & year <=1900

lpoly liabilities capitalstock if capitalstock>=0 & capitalstock<=300, n(61) bw(2.5) kernel(rectangle) nograph gen( capitalstockbin mean_liab)
keep if capitalstockbin <.
merge 1:1 capitalstockbin using `hist1'

twoway (scatter  mean_liab capitalstockbin [fw= bank_freq], ///
	mcolor(forest_green*.8) xtitle("Capital stock" " ", size(*1.2)) ytitle("")), name(scatterliab_capital, replace) ///
	title("Total assets",  box bexpand fcolor(none) size(*.9))
cd "`GRAPHDIR'"
graphexportpdf Scatter_liab_capital, name(scatterliab_capital)
exit

cd "`INDIR'"
use National_Banks_counties1890_addvar, clear
capture drop __000001
keep if year >=1870 & year <=1900
lpoly loans capitalstock if capitalstock>=0 & capitalstock<=300, n(61) bw(2.5) kernel(rectangle) nograph gen( capitalstockbin mean_loans)
keep if capitalstockbin <.
merge 1:1 capitalstockbin using `hist1'

twoway (scatter  mean_loans capitalstockbin [fw= bank_freq], ///
	mcolor(forest_green*.8) xtitle("Capital stock " " ", size(*1.2)) ytitle("") ylabel(none) ymlabel(none) ), ///
	name(scatterloans_capital, replace) ///
	title("Loans and discounts",  box bexpand fcolor(none) size(*.9)) 
cd "`GRAPHDIR'"
graphexportpdf Scatter_loans_capital, name(scatterloans_capital)

cd "`INDIR'"
use National_Banks_counties1890_addvar, clear
capture drop __000001
keep if year >=1870 & year <=1900
reg ln_tval_pc i.year if tpop>1000 & capitalstock<=300
predict demean_ln_tval_pc, resid
replace demean_ln_tval_pc = demean_ln_tval_pc + _b[_cons]
/*One point is an outlier at nearly 6 which throws the scale off*/
lpoly demean_ln_tval_pc capitalstock if capitalstock>=0 & capitalstock<=300 & demean_ln_tval_pc<5.5, n(61) bw(2.5) kernel(rectangle) nograph gen( capitalstockbin mean_ln_tval_pc)
keep if capitalstockbin <.
merge 1:1 capitalstockbin using `hist1'


twoway (scatter  mean_ln_tval_pc capitalstockbin [fw= bank_freq], ///
	mcolor(forest_green*.8) xtitle("Capital stock" " ", size(*1.2)) ytitle("")), ///
	name(scatterprod_capital, replace) ///
	title("Log total production per capita",  box bexpand fcolor(none)  size(*.9) ) 
cd "`GRAPHDIR'"
graphexportpdf Scatter_prod_capital, name(scatterprod_capital)

graph combine scatterliab_capital scatterloans_capital, ///
      cols(2)  imargin(0 0 0 0) ycommon xcommon  ///
  name(capitalstock_banks, replace)	

  graph combine scatterprod_capital scatterprod_capitalpc, ///
      cols(2)  imargin(0 0 0 0) ycommon  ///
  name(capitalstock_prod_banks, replace)	
 
 graph combine capitalstock_banks capitalstock_prod_banks, ///
 rows(2) iscale(*1.4) imargin(0 0 0 0) ysize(4) ///
 name(capital_loans_output, replace)
graph export Scatter_capital_loans_output.pdf, as(pdf) name(capital_loans_output) font(Times) replace
 
 exit


/*Try to plot densities of different counties*/

twoway (function y = (x*(10000*exp(-7.252))^(-1) * sqrt(2*3.14159)*(1))^(-1)*exp(-(log(x*(10000*exp(-7.252))^(-1)))^2/(2*(1)^2)), range(0 25)) ///
	   (function y = (x*(12000*exp(-7.252))^(-1) * sqrt(2*3.14159)*(1))^(-1)*exp(-(log(x*(12000*exp(-7.252))^(-1)))^2/(2*(1)^2)), range(0 25))
	
	
